package cn.cutie.clotcache.core;

/**
 * @Description: 命令接口
 * @Author: Cutie
 * @CreateDate: 2024/6/19 20:05
 * @Version: 0.0.1
 */
public interface Command {
    static final String CRLF = "\r\n";
    static final String OK = "OK";

    String name();
    Reply<?> exec(ClotCache cache, String[] args);

    // default method
    default String getKey(String[] args){
        return args[4];
    }
    default String getValue(String[] args){
        return args[6];
    }
    default String[] getKeys(String[] args){
        int length = (args.length - 3) / 4;
        String[] keys = new String[length];
        for (int i = 0; i < keys.length; i++) {
            keys[i] = args[4+i*4];
        }
        return keys;
    }
    default String[] getValues(String[] args){
        int len = (args.length - 3) / 4;
        String[] vals = new String[len];
        for (int i = 0; i < len; i++) {
            vals[i] = args[6 + i * 4];
        }
        return vals;
    }

    default String[] getParams(String[] args){
        int len = (args.length-3)/2;
        String[] keys = new String[len];
        for(int i=0; i<len; i++) {
            keys[i] = args[4+i*2];
        }
        return keys;
    }

    default String[] getParamsNoKey(String[] args){
        int len = (args.length-5)/2;
        String[] keys = new String[len];
        for(int i=0; i<len; i++) {
            keys[i] = args[6+i*2];
        }
        return keys;
    }


}
